package com.hqyj.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 项目:rabbitmq-230301-summer
 * 描述:
 * 时间:2023/8/3 15:10
 * 作者:admin
 * 版本:1.0
 **/
@Component
@Slf4j
public class TestTopiceListener {


    @RabbitListener(queues = "queue03")
    public void getListener(Channel channel, Message message, Map<String,Object> map) throws IOException {
            log.info("消息编号{}",message.getMessageProperties().getDeliveryTag());
        try {
            //模拟异常
            //int num = 1/0;
            //先完成增删改 业务，才手动确认消费
            //手动确认消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            log.info("手动确认消费成功");

        } catch (Exception e) {
            //判断消息是否是重新投递的
            if(message.getMessageProperties().getRedelivered()){
                //拒绝消息
                log.info("消息是重新投递的，拒绝加入队列");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                log.info("把拒绝的消息存入到数据库中或者redis缓存中");
            }else{
                log.info("手动确认消费出现异常");
                //手动确认消费失败
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }



            e.printStackTrace();
        }

    }
    @RabbitListener(queues = "queue04")
    public void getListeners(Channel channel, Message message, Map<String,Object> map) {
        log.info("queue04在消费消息");
        log.info("message消息{}",message.toString());
        log.info("消息的编号:{}," +
                        "消息的发送时间:{}," +
                        "接收消息的交换机:{}",
                message.getMessageProperties().getDeliveryTag(),
                message.getMessageProperties().getTimestamp(),
                message.getMessageProperties().getReceivedExchange()
        );
        log.info("msg{}",map.get("msg"));

    }
}
